package com.cuihanyu.newuniversityover.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.adapter.NoteAdapter;
import com.cuihanyu.newuniversityover.base.BaseFragment;
import com.cuihanyu.newuniversityover.bean.SQLBean;
import com.cuihanyu.newuniversityover.sql.DBManger;
import com.cuihanyu.newuniversityover.sql.MySQLiteHelper;
import com.cuihanyu.newuniversityover.sql.SQLiteUtil;
import com.cuihanyu.newuniversityover.util.MyLogUtil;
import com.cuihanyu.newuniversityover.views.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;


/**
 * Created by 崔汉宇 on 2017/8/7.
 */

public class Books_Fragment extends BaseFragment {
    private MySQLiteHelper helper;
    private SQLiteDatabase db;
    private RecyclerView mNote_recyclerView;
    private Cursor cursor;
    private List<SQLBean> list;
    private ImageView mBack;
    private TextView mTitle;
    private NoteAdapter adapter;

    @Override
    public View initView() {
        View view = View.inflate(activity, R.layout.fragment_elebook, null);
        if (list == null) list = new ArrayList<>();
        mBack = view.findViewById(R.id.back_ImageView);
        mBack.setVisibility(View.INVISIBLE);
        mTitle = view.findViewById(R.id.title_tv);
        mTitle.setText("笔记本");
        mNote_recyclerView = view.findViewById(R.id.Note_RecyclerView);
        mNote_recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mNote_recyclerView.addItemDecoration(new DividerGridItemDecoration(activity));
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        helper = DBManger.getIntance(activity);
        db = helper.getWritableDatabase();
        cursor = db.query(SQLiteUtil.TABLE_NAME, null, null, null, null, null, "_id desc");
        //判断游标是否为空
        if (cursor.moveToFirst()) {
            do {
                String mTime = cursor.getString(cursor.getColumnIndex("time"));
                String mChapter_name = cursor.getString(cursor.getColumnIndex("chapter_name"));
                String mChapter_small = cursor.getString(cursor.getColumnIndex("chapter_small"));
                String mNote = cursor.getString(cursor.getColumnIndex("note"));
                list.add(new SQLBean(mTime, mChapter_name, mChapter_small, mNote));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        adapter = new NoteAdapter(activity, list, this);
        mNote_recyclerView.setAdapter(adapter);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case 1:
                helper = DBManger.getIntance(activity);
                db = helper.getWritableDatabase();
                cursor = db.query(SQLiteUtil.TABLE_NAME, null, null, null, null, null, "_id desc");
                //判断游标是否为空
                list.clear();
                if (cursor.moveToFirst()) {
                    do {
                        String mTime = cursor.getString(cursor.getColumnIndex("time"));
                        String mChapter_name = cursor.getString(cursor.getColumnIndex("chapter_name"));
                        String mChapter_small = cursor.getString(cursor.getColumnIndex("chapter_small"));
                        String mNote = cursor.getString(cursor.getColumnIndex("note"));
                        MyLogUtil.e("11111111", mTime + "\n" + mChapter_name + "\n" + mChapter_small + "\n" + mNote + "\n");
                        list.add(new SQLBean(mTime, mChapter_name, mChapter_small, mNote));
                    } while (cursor.moveToNext());
                }
                cursor.close();
                db.close();
                adapter = new NoteAdapter(activity, list, this);
                adapter.notifyDataSetChanged();
                mNote_recyclerView.setAdapter(adapter);
                break;
        }
    }
}
